<template>
    <div class="book-card">
        <div class="table" width="98%">
            <div class="tr">
                <!-- 时间 -->
                <div class="date">
                    {{ create_time }}
                </div>
                <!-- 分数 -->
                <div class="busi">
                    <label v-if="record.add > 0">+{{ record.add }}</label>
                    <label v-if="record.add == 0">&nbsp;0</label>
                    <label v-if="record.add < 0">&nbsp;{{ record.add }}</label>
                </div>
                <!-- 最后总分 -->
                <div class="mark">
                    <label v-if="record.mark >= 0">&nbsp;{{ record.mark }}</label>
                    <label v-else>{{ record.mark }}</label>
                </div>
                <!-- 备注 -->
                <div class="note" @click="changeShow">
                    <label v-if="note">{{ note }}</label>
					<label v-else class="no-note">添加备注</label>
                </div>
                <label v-if="show_input" @click="cancel">
                    <div class="image">
                        <img class="img" src="/static/images/quxiao.png">
                    </div>
                </label>
                <label v-else>
                    <div class="image" @click="changeShow">
                        <img class="img" :src="note?src:''">
                    </div>
                </label>
            </div>
        </div>
        <div class="hide" v-if="show_input">
            <button class="btn" @click="updateNote">
                <label v-if="record.note">修改</label>
                <label v-else>添加</label>
            </button>
            <input v-model="note" class="input" maxlength="10" placeholder="最多输入10个字">
        </div>
    </div>
</template>

<script>
import { formatTime } from '../utils/index.js'
import { post, showModel } from '../util'
export default {
  props: ['record'],
  data () {
    return {
      create_time: formatTime(new Date(this.record.create_time)),
      show_input: false,
      note: this.record.note,
      src: '/static/images/bianji.png'
    }
  },
  methods: {
    changeShow () {
      this.show_input = !this.show_input
    },
    async updateNote () {
      try {
        const data = {
          id: this.record.id,
          note: this.note
        }

        const res = await post('/weapp/updatenote', data)
        console.log(res)
        this.show_input = false
        this.record.note = this.note
      } catch (e) {
        showModel('操作失败', '请重新提交T_T')
      }
    },
    cancel () {
      this.show_input = !this.show_input
      this.note = this.record.note
    }
  }
}
</script>

<style scoped>
    .book-card {
        background: #ffffff;
        margin-bottom: 6px;
    }
    .table {
        border: 0 solid darkgray;
        font-size: 16px;
        height: 42px;
        line-height: 42px;
    }
    .tr {
        display: flex;
        width: 100%;
    }
    .date {
        width: 40%;
        margin-left: 10px;
    }
    .busi {
        width: 10%;
        font-weight: bold;
    }
    .mark {
        width: 15%;
        margin-left: 25px;
        font-weight: bold;
    }
    .note {
        width: 16%;
        text-align: center;
        width: 60px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 15px;
        margin-left: 5px;
        line-height: 42px;
    }
    .no-note {
        text-decoration: underline;
        color: #c0c0c0;
        font-size: 14px;
    }
    .image {
        padding-top: 1px;
        float: right;
        margin-left: 5px;
    }
    .img {
        width: 13px;
        height: 13px;
        margin-right: 5px;
    }
    .hide {
        background: #f0f0f0;
        font-size: 15px;
        padding: 5px 10px 5px 30px;
    }
    .input {
        width: 60%;
        height: 30px;
        background: #ffffff;
        border: 1px solid black;
        border-radius: 5px;
        text-align: center;
    }
    .btn {
        color: white;
        background: #ea5149;
        padding-left: 15px;
        margin-left: 15px;
        border-radius: 5px;
        font-size: 14px;
        line-height: 30px;
        height: 30px;
        width: 18%;
        float: right;
    }
    ::-webkit-scrollbar {
        width: 0;
        height: 0;
        color: transparent;
    }
</style>
